//
//  MyDocument.m
//  This file is part of D&D Manager
//
//  Created by Matthew Hutton on 30/09/2006.
//  Copyright Matthew Hutton 2006 . All rights reserved.
//
//D&D Manager, a D&D Character and battle management program.
//Copyright (C) 2007, Matthew Hutton
//
//This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License along with this program; if not, write to:
//The Free Software Foundation, Inc.,
//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#import "MyDocument.h"
@implementation MyDocument

- (id)init 
{
    self = [super init];
    if (self != nil) {
		licenceShowedThisSession=NO;
		//NSLog(@"i am =%@",self);
		//appController=[[AppController alloc] init];
		//NSLog(@"document init");
        // initialization code
		origObjectsCopied=[[NSMutableArray alloc] initWithCapacity:1];
		newObjectsCopied=[[NSMutableArray alloc] initWithCapacity:1];
/*//check file extension.
		NSURL *oldUrl=[self fileURL];
		NSString *filename=[oldUrl absoluteString];
		if([[filename pathExtension] isEqualToString:@"quest"]){
			[MenuController changeExtensionToCampaignFromPath:filename
												 withDocument:self];
		}*/
    }
    return self;
}

- (NSString *)windowNibName 
{
    return @"MyDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *)windowController 
{
    [super windowControllerDidLoadNib:windowController];
    // user interface preparation code
}
-(void)windowDidBecomeKey:(NSNotification *)aNotification{
	if(![[NSUserDefaults standardUserDefaults] boolForKey:@"licenceDisplayed2"] && !licenceShowedThisSession){
		[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"licenceDisplayed"];
		//display software licence.
		//NSLog(@"displaying licence");
		if([[NSUserDefaults standardUserDefaults] objectForKey:@"licenceDisplayed2"]!=nil){
			//NSLog(@"displayed before");
			[alwaysDisplayButton setState:1];
		}else{
			[alwaysDisplayButton setState:0];
		}
		[NSApp beginSheet:softwareLicencePanel
		   modalForWindow:[aNotification object]
			modalDelegate:self
		   didEndSelector:nil
			  contextInfo:NULL];
		licenceShowedThisSession=YES;
	}	
}
-(IBAction)licenceAgreed:(id)sender{
	[softwareLicencePanel orderOut:self];
	[NSApp endSheet:softwareLicencePanel];
	//NSLog(@"state=%d",[alwaysDisplayButton state]);
	if([alwaysDisplayButton state]==1){
		//NSLog(@"setting no");
		[[NSUserDefaults standardUserDefaults] setBool:NO
												forKey:@"licenceDisplayed2"];
	}else{
		//NSLog(@"setting yes");
		[[NSUserDefaults standardUserDefaults] setBool:YES
												forKey:@"licenceDisplayed2"];
	}
	[appController setUpBattleStore];
}
-(IBAction)licenceNotAgreed:(id)sender{
	[softwareLicencePanel orderOut:self];
	[NSApp endSheet:softwareLicencePanel];
	[self updateChangeCount:2];
	//NSLog(@"state=%d",[alwaysDisplayButton state]);
	//NSLog(@"setting no");
	[[NSUserDefaults standardUserDefaults] setBool:NO
											forKey:@"licenceDisplayed2"];
	[NSApp terminate:self];
}
-(AppController *)appController{
	return appController;
}
-(int)indexInOrigObjectsOfTheObject:(NSManagedObject *)obj{
	int i;
	BOOL broken=NO;
	for(i=0;i<[origObjectsCopied count];i++){
		if([[origObjectsCopied objectAtIndex:i] isEqualTo:obj]){
			broken=YES;
			break;
		}
	}
	if(broken){
		return i;
	}else{
		return -1;
	}
}
-(NSManagedObject *)origObjectAtIndex:(int)index{
	return [origObjectsCopied objectAtIndex:index];
}
-(NSManagedObject *)newObjectAtIndex:(int)index{
	return [newObjectsCopied objectAtIndex:index];
}
-(void)addOrigObject:(NSManagedObject *)obj{
	if(obj==nil){
		NSLog(@"attempting to add nil as an original object");
		return;
	}
	[origObjectsCopied addObject:obj];
}
-(void)addNewObject:(NSManagedObject *)obj{
	if(obj==nil){
		NSLog(@"attempting to add nil as a new object");
		return;
	}
	[newObjectsCopied addObject:obj];
}
-(void)clearDuplicationArrays{
	[origObjectsCopied removeAllObjects];
	[newObjectsCopied removeAllObjects];
}
@end
